package com.Yourweibo.adapter;

import java.util.List;

import weibo4android.Count;
import weibo4android.Status;
import android.content.Context;
import android.content.Intent;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.Yourweibo.R;
import com.Yourweibo.StatusDetailActivity;
import com.Yourweibo.util.DataProcessing;

public class TimeLineAdapter extends ArrayAdapter<Status> {
	private Context context;
	private ViewHolder viewHolder;
	private List<Status> TimelineList;
 


	public TimeLineAdapter(Context context, int textViewResourceId,
			List<Status> list) {
		super(context, textViewResourceId, list);
		// TODO Auto-generated constructor stub
		this.context=context;
		TimelineList=list;
	 
	}

 

	 
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return TimelineList.size();
	}

	 

	@Override
	public Status getItem(int position) {
		// TODO Auto-generated method stub
		return TimelineList.get(position);
	}




	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position,View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		if(convertView==null){
			convertView=View.inflate(context, R.layout.main_timeline, null);
			viewHolder=new ViewHolder();
			//
			viewHolder.StatusFreamwork=(LinearLayout )convertView.findViewById(R.id.main_timeline_status_framework);
			//状态内容
		 	viewHolder.StatusText=(TextView)convertView.findViewById(R.id.main_timeline_status_text);
		 	//取得状态时间
		 	viewHolder.StatusTime=(TextView)convertView.findViewById(R.id.main_timeline_status_time);
		//	viewHolder.userName.setText(user.getName());
		 	viewHolder.StatusImageFreamwork=(RelativeLayout)convertView.findViewById(R.id.main_timeline_simage_framework);
		 	//取得用户头像
		  	viewHolder.UserPhoto=(ImageView)convertView.findViewById(R.id.main_timeline_user_photo);
		  	//取得状态图片
			viewHolder.StatusImage=(ImageView)convertView.findViewById(R.id.main_timeline_status_image);
			//取得状态信息
			viewHolder.StatusInfor=(TextView)convertView.findViewById(R.id.main_timeline_status_info);
			//转发微博视图
			viewHolder.RetweedFreamwork=(LinearLayout)convertView.findViewById(R.id.retweeted_timeline_freamwork);
			//被转发用户 状态内容
			viewHolder.RetweedStatusText=(TextView)convertView.findViewById(R.id.retweeted_timeline_status_text);
			//被转发用户 取得状态时间
		 	viewHolder.RetweedStatusTime=(TextView)convertView.findViewById(R.id.retweeted_timeline_status_time);
		 	//被转发用户 取得状态时间
		 	viewHolder.RetweedStatusImage=(ImageView)convertView.findViewById(R.id.retweeted_timeline_status_image);
			convertView.setTag(viewHolder);
		} else{
			viewHolder=(ViewHolder)convertView.getTag();
			
		}

		
		
		
		
		
		
		final Status status=TimelineList.get(position);
		//异步加载头像
		final String photoUrl=status.getUser().getProfileImageURL().toString();
		viewHolder.UserPhoto.setTag(photoUrl);
		DataProcessing.loadHttpImage(photoUrl, viewHolder.UserPhoto,convertView.getContext());
    	
    	final String photoUrl2=status.getThumbnail_pic();
		if(photoUrl2!=null&&!photoUrl2.equals("")){
			//异步加载状态图片
		
			DataProcessing.loadHttpImage(photoUrl2,viewHolder.StatusImage,convertView.getContext());
			viewHolder.StatusImageFreamwork.setVisibility(View.VISIBLE);
			
		}else{
			viewHolder.StatusImageFreamwork.setVisibility(View.GONE);	
			
		}
		viewHolder.StatusImage.setTag(status.getBmiddle_pic());
		viewHolder.StatusImage.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DataProcessing.LoadlargeImage(v,(String)v.getTag());
			}});
		//加载状态 text 时间
	 
		//设置高亮
	 
		 
		viewHolder.StatusText.setText(DataProcessing.statusSetHighligth(status));
		//viewHolder.StatusText.setMovementMethod(LinkMovementMethod.getInstance());
	 	viewHolder.StatusTime.setText(status.getCreatedAt().toLocaleString());
 
	 	 
	 	
	 	
	 	
		//是否有转发
	 
			
		Status reTweetedStatus=	status.getRetweeted_status();
			
		if(reTweetedStatus!=null){
			viewHolder.RetweedFreamwork.setVisibility(View.VISIBLE);
			viewHolder.RetweedStatusText.setText(DataProcessing.statusSetHighligth(reTweetedStatus));
			viewHolder.RetweedStatusText.setMovementMethod(LinkMovementMethod.getInstance());
			viewHolder.RetweedStatusTime.setText(status.getCreatedAt().toLocaleString());
	    	final String photoUrl3=reTweetedStatus.getThumbnail_pic();
	    	if(photoUrl3!=null&&!photoUrl3.equals("")){
	    		DataProcessing.loadHttpImage(photoUrl3,viewHolder.RetweedStatusImage,convertView.getContext());
	      	}		
	    	viewHolder.RetweedStatusImage.setTag(reTweetedStatus.getBmiddle_pic());
	    	viewHolder.RetweedStatusImage.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					DataProcessing.LoadlargeImage(v,(String)v.getTag());
				}});
	 
		}else{
			viewHolder.RetweedFreamwork.setVisibility(View.GONE);
			
		}
		//每条微博的单击事件
		convertView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(context,StatusDetailActivity.class);
				intent.putExtra("CurrentStatus", status);
				context.startActivity(intent);
			 
				
			}});
 
  
	 //	viewHolder.StatusInfor.setText(TimelineList.get(position).getUser().getCreatedAt().toGMTString());
   	return convertView;
	}
	
 
	 
	
	
	static class ViewHolder{
		ImageView UserPhoto;
		TextView UserName;
		TextView StatusText;
		TextView StatusInfor;
		TextView StatusTime;
		ImageView StatusImage;
		RelativeLayout StatusImageFreamwork;
		LinearLayout StatusFreamwork;
		
		LinearLayout RetweedFreamwork;
		
		TextView RetweedStatusText;
		TextView RetweedStatusInfor;
		TextView RetweedStatusTime;
		ImageView RetweedStatusImage;
	}
}
